package com.aitaoxue.palx.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.util.TDevice;

public class WaitDialog extends Dialog{
	private TextView _message;

	public WaitDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		init(context);
	}

	public WaitDialog(Context context, int theme) {
		super(context, theme);
		init(context);
	}

	public WaitDialog(Context context) {
		super(context);
		init(context);
	}
	
	public static boolean dismiss(WaitDialog dialog){
		if(dialog!=null){
			dialog.dismiss();
			return false;
		}else{
			return true;
		}
	}
	
	public static void hide(Context context){
		if(context instanceof DialogControl){
			((DialogControl)context).hideWaitDialog();
		}
	}
	
	public static boolean hide(WaitDialog dialog){
		if(dialog!=null){
			dialog.hide();
			return false;
		}else{
			return true;
		}
	}
	
	public static void show(Context context) {
		if (context instanceof DialogControl)
			((DialogControl) context).showWaitDialog();
	}

	public static boolean show(WaitDialog waitdialog) {
		boolean flag;
		if (waitdialog != null) {
			waitdialog.show();
			flag = false;
		} else {
			flag = true;
		}
		return flag;
	}
	
	private void init(Context context){
		setCancelable(false);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		View view=LayoutInflater.from(context).inflate(R.layout.dialog_wait, null);
		_message=(TextView) view.findViewById(R.id.waiting_tv);
		setContentView(view);
	}
	
	public void setMessage(int message) {
		_message.setText(message);
	}

	public void setMessage(String message) {
		_message.setText(message);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(TDevice.isTablet()){
			int i=(int) TDevice.dpToPixel(360F);
			if(i<TDevice.getScreenWidth()){
				WindowManager.LayoutParams params = getWindow()
						.getAttributes();
				params.width = i;
				getWindow().setAttributes(params);
			}
		}
	}
	
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		this.dismiss();
	}
	
}
